package github.sf.fw.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonObject;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * Json工具
 */
public class Jsons {
    public static class Jackson {
        private Jackson() {
        }

        public static String toJson(Object obj) {
            if (obj == null) {
                return "{}";
            }
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(obj);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

        public static <T> T fromJson(String json, Class<T> clazz) {
            if (json == null || "".equals(json.trim())) {
                return null;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.readValue(json, clazz);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static class Gson {
        private Gson() {
        }

        public static String toJson(Object obj) {
            if (obj == null) {
                return "{}";
            }
            com.google.gson.Gson gson = new GsonBuilder().serializeNulls().create();
            return gson.toJson(obj);
        }

        public static JsonObject toJsonObject(Object obj) {
            if (obj == null) {
                return new JsonObject();
            }
            com.google.gson.Gson gson = new GsonBuilder().serializeNulls().create();
            return gson.toJsonTree(obj).getAsJsonObject();
        }

        public static String toPrettyJson(Object obj) {
            if (obj == null) {
                return "{}";
            }
            com.google.gson.Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
            return gson.toJson(obj);
        }

        /**
         * 暂不支持数组和集合类型
         */
        public static <T> T fromJson(String json, Class<T> clazz) {
            if (json == null || "".equals(json.trim())) {
                return null;
            }
            com.google.gson.Gson gson = new GsonBuilder().serializeNulls().create();
            return gson.fromJson(json, clazz);
        }


        public static <T> T fromJson(String json, TypeRef<T> typeRef) {
            if (json == null || "".equals(json.trim())) {
                return null;
            }
            com.google.gson.Gson gson = new GsonBuilder().serializeNulls().create();
            return gson.fromJson(json, typeRef.getRawType());
        }

        public static <T> T fromJson(String json, Class<T> clazz, Map<Type, JsonDeserializer<?>> deserializers) {
            if (json == null || "".equals(json.trim())) {
                return null;
            }
            GsonBuilder gsonBuilder = new GsonBuilder();
            for (Map.Entry<Type, JsonDeserializer<?>> typeClassEntry : deserializers.entrySet()) {
                gsonBuilder.registerTypeAdapter(typeClassEntry.getKey(), typeClassEntry.getValue());
            }
            com.google.gson.Gson gson = gsonBuilder.serializeNulls().create();
            return gson.fromJson(json, clazz);
        }

        public static <T> T fromJsonObject(JsonObject json, Class<T> clazz) {
            if (json == null || json.isJsonPrimitive()) {
                return null;
            }
            com.google.gson.Gson gson = new GsonBuilder().serializeNulls().create();
            return gson.fromJson(json, clazz);
        }
    }


}
